/**
 * Copyright 2008 Mathias Doenitz, http://lis.to/
 *
 * This file is part of the lis.to java desktop client. The lis.to java desktop client is free software: you can
 * redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free
 * Software Foundation, either version 3 of the License, or (at your option) any later version.
 *
 * The lis.to java desktop client is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with the lis.to java desktop client.
 * If not, see http://www.gnu.org/licenses/
 */

package listo.utils.types;

import com.thoughtworks.xstream.converters.SingleValueConverter;
import listo.utils.exceptions.IllegalFormattingException;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Immutable date and time representation.
 */
public class DateTime {
    
    private static final DateFormat DEFAULT_FORMAT = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss Z");

    private final long ticks;

    public DateTime() {
        this(new Date());
    }

    public DateTime(Date date) {
        ticks = date.getTime();
    }

    public Date getDate() {
        return new Date(ticks);
    }

    public long getTicks() {
        return ticks;
    }

    public static DateTime parse(String text, DateFormat dateFormat) {
        try {
            return new DateTime(dateFormat.parse(text));
        }
        catch (ParseException e) {
            throw new IllegalFormattingException(e);
        }
    }

    public static DateTime valueOf(String text) {
        return parse(text, DEFAULT_FORMAT);
    }

    public int hashCode() {
        return (int) (ticks ^ (ticks >>> 32));
    }

    public boolean equals(Object obj) {
        return obj instanceof DateTime && ((DateTime) obj).ticks == ticks;
    }

    public boolean before(DateTime other) {
        return ticks < other.ticks;
    }

    public boolean after(DateTime other) {
        return ticks > other.ticks;
    }

    public String format(DateFormat dateFormat) {
        return dateFormat.format(getDate());
    }

    public String toString() {
        return DEFAULT_FORMAT.format(getDate());
    }

    /**
     * A simple xml converter using HEX values.
     */
    public static class Converter implements SingleValueConverter {

        public boolean canConvert(Class type) {
            return type == DateTime.class;
        }

        public String toString(Object obj) {
            return obj.toString();
        }

        public Object fromString(String str) {
            return DateTime.valueOf(str);
        }
    }
}
